// shallowReactive 和 reactive 函数

// shallowreadonly 和 readonly 函数

const  reactiveHandler =  {
    get(target, prop) {
        if(prop === 'isReactive') return true
        console.log('get 数据劫持 ...');
        return Reflect.get(target, prop);
    },
    set(target, prop, value) {
        console.log('set  数据劫持 ...');
        return Reflect.set(target, prop, value)
    },
    deleteProperty(target, prop) {
        console.log('delete 删除数据劫持 ...');
        return Reflect.deleteProperty(target, prop)
    }
}

function shallowReactive(target) {
    // 如果 target 是对象类型
    if(target && typeof target === 'object') {
        return new Proxy(target, reactiveHandler)
    }
    //  如果是基本数据类型就直接返回
    return target; 
}

function reactive(target) {
    if(target && typeof target !== 'object'){
        return target
    }

    if(Array.isArray(target)) {
        // 如果是数组
        target.forEach((item, index) => {
            target[index] = reactive(item)
        })
    }else {
        // 如果是对象
        Object.keys(target).forEach(item => {
            target[item] = reactive(target[item])
        })
    }

    return new Proxy(target, reactiveHandler)
}

// shallowReadOnly 和 readonly 
const readonlyHandler = {
    get(target, prop) {
        if(prop === 'isReadonly') return true
        console.log('get 数据劫持 ...')
        return Reflect.get(target, prop)
    },
    set(target, prop, value) {
        console.warn('不能支持修改值 ...')
    },
    deleteProperty(targt, prop, value) {
        console.warn('不能支持删除值 ...')
    }
}

/**
 *   浅度只读 
 *   target 目标对象
 */
function shallowReadonly(target) {
    if(!target || typeof target !== 'object') {
        return target 
    }
    return new Proxy(target, readonlyHandler)
}

/**
 * 深度只读 readonly 
 * @param {目标对象} target 
 * @returns 
 */
function readonly(target) {
    if(!target || typeof target !== 'object') {
        return target 
    }
    if(Array.isArray(target)) {
        target.forEach((item, index) => {
            target[index] = readonly(item)
        })
    }else {
        Object.keys(target).forEach(item => {
            target[item] = readonly(target[item])
        })
    }
    return new Proxy(target, readonlyHandler)
}

// shallowRef 和 ref 

function shallowRef(target) {
    return {
        _value: target, 
        get value() {
            console.log('劫持到获取数据');
            return this._value
        },
        set value(val) {
            console.log('劫持到设置数据');
            this._value = val
            return this._value
        }
    }
}

function ref(target) {
    target = reactive(target)
    return {
        _value: target, 
        _is_ref: true,
        get value() {
            console.log('劫持到获取数据');
            return this._value
        },
        set value(val) {
            console.log('劫持到设置数据');
            this._value = val
        },
        get isRef() {
            return this._is_ref
        }
    }
}

// isRef,  isReactive, isReadonly, isProxy

function isRef(obj) {
    return obj && obj.isRef
}

function isReactive(obj) {
    return obj && obj.isReactive
}

function isReadonly(obj) {
    return obj && obj.isReadonly
}

function isProxy(obj) {
    return isReactive(obj) || isReadonly(obj)
}





